package com.zhkj.shixi.tvfilemanager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.util.Log;
import android.widget.Toast;

import com.zhkj.shixi.tvfilemanager.Interface.MenuCtrl;
import com.zhkj.shixi.tvfilemanager.view.GrideViewPager;
import com.zhkj.shixi.tvfilemanager.view.ViewDataFlush;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import static com.zhkj.shixi.tvfilemanager.FileListActivity.isGridView;
import static com.zhkj.shixi.tvfilemanager.StaticClasses.fileManager;

/**
 * Created by suqiuyu on 19-8-12.
 */

public class FileMenuCtrl implements MenuCtrl {
    private List<File> mSelectedList;
    private List<File> mCopyedList;
    private Activity mActivity;

    public FileMenuCtrl(Activity activity) {
        mActivity = activity;
        mSelectedList = new ArrayList<File>();
        mCopyedList = new ArrayList<File>();
    }

    @Override
    public void addFile(File file) {
        mSelectedList.add(file);
    }

    @Override
    public void removeFile(File file) {
        mSelectedList.remove(file);
    }

    public void removeAllFile() {
        mSelectedList.clear();
    }

    @Override
    public void deleteFile() {
        for (File e : mSelectedList) {
            delete(e);
        }
        mSelectedList.clear();
    }

    private void delete(File file){
        if (file.isDirectory()) {
            for (File e : file.listFiles()) {
                delete(e);
            }
            file.delete();
        } else {
            Log.i("delete: ", file.getAbsolutePath() + file.delete());
            //file.delete();
        }
    }

    @Override
    public void copyFile() {
        mCopyedList.clear();
        for (File e : mSelectedList) {
            Log.i("copyFile: ", e.getPath());
            mCopyedList.add(e);
        }
    }

    @Override
    public void pasteFile() {
        for (File e : mCopyedList) {
            paste(e,fileManager.getCurrentURL());
        }
    }

    private void paste(File originFile, String pasteDir){
        File newFile = new File(pasteDir,originFile.getName());
        if (originFile.isDirectory()) {
            newFile.mkdir();
            for (File e : originFile.listFiles()) {
                paste(e,pasteDir + "/" + originFile.getName());
            }
        } else {
            if (!originFile.getAbsolutePath().equals(newFile.getAbsolutePath())) {
                try {
                    FileInputStream fin = new FileInputStream(originFile);
                    FileOutputStream fout = new FileOutputStream(newFile);
                    BufferedInputStream bis = new BufferedInputStream(fin);
                    BufferedOutputStream bos = new BufferedOutputStream(fout);
                    int n = fin.available()/5;
                    byte b[] = new byte[n];
                    int count = 0;
                    while((count = bis.read(b,0,n)) != -1)
                        bos.write(b,0,count);
                    bis.close();
                    bos.flush();
                    bos.close();
                    fin.close();
                    fout.flush();
                    fout.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void renameFile(String name, File originFile) {
        Log.i("renameFile", name);
        Log.i("renameFile-originFile", originFile.getAbsolutePath());
        File newFile = new File(originFile.getParent(),name);
        Log.i("renameFile-newFile", newFile.getAbsolutePath() + " " + originFile.renameTo(newFile));
    }

    public void renameDialog(){
        if (mSelectedList.size() == 1) {
            final File file = mSelectedList.get(0);
            AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
            final AlertDialog dialog = builder.create();
            View dialogView = View.inflate(mActivity, R.layout.rename_dialog, null);
            final EditText renameEditText = dialogView.findViewById(R.id.renameEditText);
            renameEditText.setText(mSelectedList.get(0).getName());
            Button btnCancel = dialogView.findViewById(R.id.btnCancel);
            Button btnOK = dialogView.findViewById(R.id.btnOK);
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    GrideViewPager.fileList = FileManager.getTypeFiles(fileManager.getCurrentURL(),fileManager.getType());
                    ViewDataFlush.flushData(GrideViewPager.fileList,isGridView);
                }
            });
            btnOK.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("renameDialog", "onClick: ");
                    Log.i("renameDialog", renameEditText.getText().toString());
                    renameFile(renameEditText.getText().toString(),file);
                    GrideViewPager.fileList = FileManager.getTypeFiles(fileManager.getCurrentURL(),fileManager.getType());
                    ViewDataFlush.flushData(GrideViewPager.fileList,isGridView);
                    dialog.dismiss();
                }
            });
            dialog.setView(dialogView);
            dialog.show();
        }
        mSelectedList.clear();
    }

    @Override
    public void clickFile(File file) {
        if (file.isFile()) {
            Intent intent = new Intent();
            ComponentName cn;
            if (FileManager.MUSIC_TYPE == fileManager.getType()) {
                cn = new ComponentName("com.zhkj.shixi.tvplayer",
                        "com.zhkj.shixi.tvplayer.MusicPlayer.AudioActivity");
            }else if (FileManager.VIDEO_TYPE == fileManager.getType()) {
                cn = new ComponentName("com.zhkj.shixi.tvplayer",
                        "com.zhkj.shixi.tvplayer.VideoPlayer.VideoPlayerActivity");
            }else {
                cn = new ComponentName("com.zhkj.shixi.tvplayer",
                        "com.zhkj.shixi.tvplayer.PicturePlayer.PicPlayActivity");
            }
            try {
                intent.setComponent(cn);
                Log.e("url", file.getAbsolutePath());
                intent.putExtra("url", file.getAbsolutePath());
                mActivity.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                Log.i("click", e.getMessage());
                Toast.makeText(mActivity,e.getMessage(),Toast.LENGTH_LONG).show();
            }
        } else {
            fileManager.setCurrentURL(file.getAbsolutePath());
        }
    }
}
